package com.controller
{
	
	
	
	import com.model.valueObjects.VOInvoiceMaster;
	import com.model.valueObjects.VOSplitMaster;
	import com.service.InvoiceMasterDetails;
	import com.view.FilterSelector;
	
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.Base64Decoder;
	
	public class InvoiceMasterController extends AbstractController
	{
		import com.model.Model;
		[Inject]
		public var invoiceMasterDetails:InvoiceMasterDetails;
		
		[EventHandler(event="InvoiceMasterEvent.GET_ALL")]
		public function getAllInvoice():void{
		/*	for each(var obj:VOSplitMaster in model.SplitTableMasterDetails){
				for(var i:int=0;i<model.currentFolders.length;i++){
					if(obj.description==model.currentFolders[i]){
						obj.isSelected=true;
						break;
					}
				}
			}*/
			sh.executeServiceCall(invoiceMasterDetails.getAllInvoices(model.currentDatabase), getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.InvoiceMasterDetails=event.result as ArrayCollection;
				
			var fs:FilterSelector = new FilterSelector();
			fs.filterField = "folder_id";
			fs.addFilterTypes(model.currentFolders);
			model.InvoiceMasterDetails.filterFunction = fs.generateFilterFunction();
			model.InvoiceMasterDetails.refresh(); 
			model.FolderMaster.refresh();
			
		//	model.InvoiceMasterDetails.sort = new Sort();
		//	model.InvoiceMasterDetails.sort.fields = [new SortField("folder_id", true, true)];
		//	model.InvoiceMasterDetails.refresh();
			
		}
		[EventHandler(event="InvoiceMasterEvent.ADD_INVOICE", properties="invoiceVo,imageString")]
		public function addInvoice(invoiceVo:VOInvoiceMaster,imageString:String):void{
			sh.executeServiceCall(invoiceMasterDetails.saveInvoice(invoiceVo,imageString,model.currentDatabase),addInvoiceResult, handleFault);
		}
		
		private function addInvoiceResult(event:ResultEvent):void
		{
			Alert.show("Invice Added");
			model.SplitTableMasterDetails=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			getAllInvoice();
			getAllImage();
		}
		[EventHandler(event="InvoiceMasterEvent.EDIT_INVOICE", properties="invoiceVo,imageString")]
		public function editInvoice(invoiceVo:VOInvoiceMaster,imageString:String):void{
			sh.executeServiceCall(invoiceMasterDetails.editInvoice(invoiceVo,imageString,model.currentDatabase), editInvoiceResult, handleFault );
		}
		
		private function editInvoiceResult(event:ResultEvent):void
		{
			Alert.show("Invoice Updated");
			model.SplitTableMasterDetails=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			getAllInvoice();
			getAllImage();
		}
		[EventHandler(event="InvoiceMasterEvent.DELETE_INVOICE", properties="invoiceNos")]
		public function deleteInvoice(invoiceNos:String):void{
			sh.executeServiceCall(invoiceMasterDetails.deleteInvoice(invoiceNos,model.currentDatabase), deleteInvoiceResult, handleFault );
		}
		
		private function deleteInvoiceResult(event:ResultEvent):void
		{
			Alert.show("Invoice Deleted");
			model.SplitTableMasterDetails=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			getAllInvoice();
		}
		[EventHandler(event="InvoiceMasterEvent.MOVE_INVOICE", properties="invoiceNos,moveFolder")]
		public function moveInvoices(invoiceNos:String,moveFolder:String):void{
			sh.executeServiceCall(invoiceMasterDetails.moveInvoice(invoiceNos,moveFolder,model.currentDatabase), moveInvoiceResult, handleFault );
		}
		
		private function moveInvoiceResult(event:ResultEvent):void
		{
			Alert.show("Invoice are Moved");
			model.SplitTableMasterDetails=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			getAllInvoice();
		}
		
		[EventHandler(event="InvoiceMasterEvent.GET_IMAGE", properties="invoiceNos")]
		public function getImage(invoiceNos:String):void{
			model.showImageLayout=true;
			sh.executeServiceCall(invoiceMasterDetails.getImageByInvice(invoiceNos,model.currentDatabase), getImageResult, handleFault );
		}
		
		private function getImageResult(event:ResultEvent):void
		{
			if(event.result!='false'){
			var imgStr:String=event.result as String;
			model.showImageLayout=true;
			model.invoiceImage=imageDecode(imgStr);
			}else{
				model.showImageLayout=false;
			}
			CursorManager.removeBusyCursor();
		}
		
		[EventHandler(event="InvoiceMasterEvent.GET_ALL_IMAGE")]
		public function getAllImage():void{
			sh.executeServiceCall(invoiceMasterDetails.getAlImags(model.currentDatabase), getAllImageResult, handleFault );
		}
		private function getAllImageResult(event:ResultEvent):void
		{
			model.allInvoiceImages= event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		[EventHandler(event="InvoiceMasterEvent.REMOVE_IMAGE", properties="invoiceNos")]
		public function removeImage(invoiceNos:String):void{
			sh.executeServiceCall(invoiceMasterDetails.removeImageByInvice(invoiceNos,model.currentDatabase), removeImageResult, handleFault );
		}
		
		private function removeImageResult(event:ResultEvent):void
		{
			model.showImageLayout=false;
			Alert.show("Image Removed");
			getAllImage();
		}
		[EventHandler(event="InvoiceMasterEvent.ADD_IMAGE", properties="invoiceNos,imageString")]
		public function addImage(invoiceNos:String,imageString:String):void{
			sh.executeServiceCall(invoiceMasterDetails.imageUpload(invoiceNos,imageString,model.currentDatabase), addImageResult, handleFault );
		}
		private function addImageResult(event:ResultEvent):void
		{
			model.showImageLayout=true;
			var imgStr:String=event.result as String;
			model.invoiceImage=imageDecode(imgStr);
			getAllImage();
		}
		
		[EventHandler(event="InvoiceMasterEvent.DATA_UPLOAD", properties="invoiceArray,folderArray,vendorArray,categoryArray,customerArray,expenseTypeArray,locationArray,paymentTypeArray,projectCodeArray,taxCatagoryArray,userArray,dbname,clientId")]
		public function dataUpload(invoiceArray:Array,folderArray:Array,vendorArray:Array,categoryArray:Array,customerArray:Array,expenseTypeArray:Array,locationArray:Array,paymentTypeArray:Array,projectCodeArray:Array,taxCatagoryArray:Array,userArray:Array,dbname:String,clientId:String):void{
			
			sh.executeServiceCall(invoiceMasterDetails.dataUpload(invoiceArray,folderArray,vendorArray,categoryArray,customerArray,expenseTypeArray,locationArray,paymentTypeArray,projectCodeArray,taxCatagoryArray,userArray,dbname,clientId), dataUploadResult, handleFault );
		}
		private function dataUploadResult(event:ResultEvent):void
		{
			model.ClientMasterDetails=event.result as ArrayCollection
			model.ClientMasterDetails.refresh();
			Alert.show("Data Uploaded successfully");
		}
		
		
		[EventHandler(event="InvoiceMasterEvent.INLINE_EDIT", properties="item,itemId,invoiceNo")]
		public function inlineEditInvoice(item:String,itemId:String,invoiceNo:Number):void{
			sh.executeServiceCall(invoiceMasterDetails.inlineUpdateInvoice(item,itemId,invoiceNo,model.currentDatabase), inlineEditInvoiceResult, handleFault );
		}
		private function inlineEditInvoiceResult(event:ResultEvent):void
		{
			getAllInvoice();
			CursorManager.removeBusyCursor();
			Alert.show("Invoice Updated successfully");
			
		}
		[EventHandler(event="InvoiceMasterEvent.ADD_INLINE_NEW_FOLDER", properties="description,invoiceNo")]
		public function inlineAddNewFolder(description:String,invoiceNo:Number):void{
			sh.executeServiceCall(invoiceMasterDetails.addNewInlineFolder(description,invoiceNo,model.currentDatabase), inlineAddNewFolderResult, handleFault );
		}
		private function inlineAddNewFolderResult(event:ResultEvent):void
		{
			model.FolderMaster=event.result as ArrayCollection;	
			sh.executeServiceCall(invoiceMasterDetails.recreatSplitTable(model.currentDatabase), inlineAddNewFolderSplitResultResult, handleFault );
		}
		private function inlineAddNewFolderSplitResultResult(event:ResultEvent):void
		{
			model.SplitTableMasterDetails=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			getAllInvoice();
		}
		private function imageDecode(image:String):ByteArray{
			var byteArr:ByteArray;
			var base64Dec:Base64Decoder = new Base64Decoder();
			var str:String=String(image);
			base64Dec.decode(str);
			byteArr = base64Dec.toByteArray();
			return byteArr;
		}

		private function handleFault(event:FaultEvent):void
		{
			Alert.show( 'Error '+event.fault.name, "Error" );
			CursorManager.removeBusyCursor();
		}
		
	}
}